package org.celery.modules.goods.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.celery.modules.goods.entity.GoodsStyle;
import org.celery.modules.goods.vo.GoodsStyleVO;
import org.celery.modules.goods.wrapper.GoodsStyleWrapper;
import org.celery.modules.goods.service.IGoodsStyleService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品款式表 控制器
 *
 * @author Celery
 * @since 2022-03-30
 */
@RestController
@AllArgsConstructor
@RequestMapping("celery-goods/goods-style")
@Api(value = "商品款式表", tags = "商品款式表接口")
public class GoodsStyleController extends BladeController {

	private final IGoodsStyleService goodsStyleService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入goodsStyle")
	public R<GoodsStyleVO> detail(GoodsStyle goodsStyle) {
		GoodsStyle detail = goodsStyleService.getOne(Condition.getQueryWrapper(goodsStyle));
		return R.data(GoodsStyleWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 商品款式表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入goodsStyle")
	public R<IPage<GoodsStyleVO>> list(GoodsStyle goodsStyle, Query query) {
		IPage<GoodsStyle> pages = goodsStyleService.page(Condition.getPage(query), Condition.getQueryWrapper(goodsStyle));
		return R.data(GoodsStyleWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 商品款式表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入goodsStyle")
	public R<IPage<GoodsStyleVO>> page(GoodsStyleVO goodsStyle, Query query) {
		IPage<GoodsStyleVO> pages = goodsStyleService.selectGoodsStylePage(Condition.getPage(query), goodsStyle);
		return R.data(pages);
	}

	/**
	 * 新增 商品款式表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入goodsStyle")
	public R save(@Valid @RequestBody GoodsStyle goodsStyle) {
		return R.status(goodsStyleService.save(goodsStyle));
	}

	/**
	 * 修改 商品款式表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入goodsStyle")
	public R update(@Valid @RequestBody GoodsStyle goodsStyle) {
		return R.status(goodsStyleService.updateById(goodsStyle));
	}

	/**
	 * 新增或修改 商品款式表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入goodsStyle")
	public R submit(@Valid @RequestBody GoodsStyle goodsStyle) {
		return R.status(goodsStyleService.saveOrUpdate(goodsStyle));
	}

	
	/**
	 * 删除 商品款式表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(goodsStyleService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 商品款式批量提交
	 */
	@PostMapping("/submit-batch")
	@ApiOperationSupport(order = 8)
	@Transactional
	@ApiOperation(value = "商品款式批量提交", notes = "传入goodsStyle")
	public R submitBatch(@Valid @RequestBody List<GoodsStyle> goodsStyleList, String goodsId) {
		List<GoodsStyle> originalGoodsStyleList = goodsStyleService.list(Wrappers.<GoodsStyle>lambdaQuery()
				.eq(GoodsStyle::getGoodsId, goodsId)
		);

		for (GoodsStyle goodsStyle: goodsStyleList) {
			if (Func.isEmpty(goodsStyle.getId())) {
				goodsStyleService.save(goodsStyle);
			} else {
				goodsStyleService.saveOrUpdate(goodsStyle);
				originalGoodsStyleList.removeIf(originalGoodsStyle -> originalGoodsStyle.getId().equals(goodsStyle.getId()));
			}
		}
		if (Func.isNotEmpty(originalGoodsStyleList)) {
			goodsStyleService.removeByIds(originalGoodsStyleList.stream().map(GoodsStyle::getId).collect(Collectors.toList()));
		}

		return R.status(true);
	}

	
}
